﻿using System;
using System.Drawing;
using System.Windows.Forms;
using LevelEditor.Document;
using LevelEditor.View;
using WeifenLuo.WinFormsUI.Docking;

namespace LevelEditor.Forms
{
    public partial class PaletteForm : DockContent
    {
        #region Fields

        int LayerIndex;

        bool gridVisible = true;

        decimal gridThickness = 1;

        #endregion

        #region Constructors

        public PaletteForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        public delegate void RemoveTabEventHandler(object sender, EventArgs e);
        public event RemoveTabEventHandler RemoveTab;
        protected void OnRemoveTab(EventArgs e)
        {
            if (RemoveTab != null)
                RemoveTab(this, e);
        }

        public delegate void AddTabEventHandler(object sender, EventArgs e);
        public event AddTabEventHandler AddTab;
        protected void OnAddTab(EventArgs e)
        {
            if (AddTab != null)
                AddTab(this, e);
        }

        public delegate void OptionsEventHandler(object sender, EventArgs e);
        public event OptionsEventHandler Options;
        protected void OnOptions(EventArgs e)
        {
            if (Options != null)
                Options(this, e);
        }

        #endregion

        #region Methods

        public void RebuildPages(MainForm form)
        {
            foreach (TabPage tp in tabs.TabPages)
                tp.Dispose();
            tabs.TabPages.Clear();
            Layer currentLayer = form.Level.Layers[LayerIndex];
            for (int i = 0; i < currentLayer.TileSets.Count; ++i)
            {
                TabPage newTabPage = new TabPage("" + i);
                newTabPage.AutoScroll = true;
                PaletteControl pc = new PaletteControl();
                pc.ForeColor = ForeColor;
                pc.BackColor = BackColor;
                pc.GridThickness = gridThickness;
                pc.GridVisible = gridVisible;
                pc.Bitmap = new Bitmap(form.LayerViewControl.Views[LayerIndex].TileSetImages[i]);
                pc.TileSize = currentLayer.TileSize;
                pc.Reset();
                newTabPage.Controls.Add(pc);
                tabs.TabPages.Add(newTabPage);
            }
        }

        public void ResetToCurrentLayer(MainForm form)
        {
            if (form.CurrentLayer != LayerIndex)
            {
                LayerIndex = form.CurrentLayer;
                RebuildPages(form);
            }
            else
            {
                if (tabs.TabPages.Count != form.Level.Layers[LayerIndex].TileSets.Count)
                    RebuildPages(form);
            }
        }

        public void ResetBitmap(MainForm form, int tileSetIndex)
        {
            if (tileSetIndex < tabs.TabPages.Count && tileSetIndex >= 0)
            {
                PaletteControl pc = (tabs.TabPages[tileSetIndex].Controls[0] as PaletteControl);
                pc.Bitmap.Dispose();
                pc.Bitmap = new Bitmap(form.LayerViewControl.Views[LayerIndex].TileSetImages[tileSetIndex]);
                pc.TileSize = form.Level.Layers[LayerIndex].TileSize;
                pc.Reset();
            }
        }

        public void SetPaletteGridColor(Color gridColor)
        {
            ForeColor = gridColor;
            foreach (TabPage tp in tabs.TabPages)
                (tp.Controls[0] as PaletteControl).ForeColor = gridColor;
            Invalidate();
        }

        public void SetPaletteBackColor(Color backColor)
        {
            BackColor = backColor;
            foreach (TabPage tp in tabs.TabPages)
                (tp.Controls[0] as PaletteControl).BackColor = backColor;
            Invalidate();
        }

        public bool GetGridVisible()
        {
            return gridVisible;
        }

        public void SetGridVisible(bool visible)
        {
            gridVisible = visible;
            foreach (TabPage tp in tabs.TabPages)
                (tp.Controls[0] as PaletteControl).GridVisible = gridVisible;
        }

        public decimal GetGridThickness()
        {
            return gridThickness;
        }

        public void SetGridThickness(decimal thickness)
        {
            gridThickness = thickness;
            foreach (TabPage tp in tabs.TabPages)
                (tp.Controls[0] as PaletteControl).GridThickness = gridThickness;
        }

        #endregion

        #region Events

        private void PaletteForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void tabs_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                for (int i = 0; i < tabs.TabPages.Count; ++i)
                {
                    Rectangle rect = tabs.GetTabRect(i);
                    if (rect.Contains(e.Location))
                    {
                        tabs.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OnOptions(e);
        }

        private void removeTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tabs.TabPages.Count > 1)
            {
                OnRemoveTab(e);
            }
        }

        private void addTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OnAddTab(e);
        }

        #endregion

    }
}
